package com.tmt.wechat;

import java.io.File;
import java.io.FileInputStream;
import java.security.KeyStore;

import javax.net.ssl.KeyManagerFactory;

import com.swak.wechat.WechatConfig;

import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;

/**
 * 默认的配置实现
 * 
 * @author lifeng
 */
public class WechatConfigImpl implements WechatConfig {

	// 基本 - 登录
	private String appId;
	private String secret;
	private String apiDomain;

	// 消息接收
	private String token;
	private String aesKey;
	private String srcId;
	private Object setting;

	// 支付
	private String mchApp;
	private String mchId;
	private String mchName;
	private String mchKey;
	private String payNotifyUrl;
	private String refundNotifyUrl;
	private String keyPath;
	private SslContext sslContext;

	private WechatConfigImpl() {
		this.appId = "xxxx";
		this.secret = "xxxx";
		this.apiDomain = "https://api.catax.cn";
		this.token = "";
		this.srcId = "";
		this.mchApp = "";
		this.mchId = "";
		this.mchName = "";
		this.mchKey = "";
		this.payNotifyUrl = this.apiDomain + "/api/notify/pay";
		this.refundNotifyUrl = "";
		this.keyPath = "";
	}

	@Override
	public String getAppId() {
		return appId;
	}

	@Override
	public String getSecret() {
		return secret;
	}

	@Override
	public String getToken() {
		return token;
	}

	@Override
	public String getAesKey() {
		return aesKey;
	}

	@Override
	public String getSrcId() {
		return srcId;
	}

	@Override
	public Object getSetting() {
		return setting;
	}

	@Override
	public String getMchApp() {
		return this.mchApp;
	}

	@Override
	public String getMchId() {
		return mchId;
	}

	@Override
	public String getMchName() {
		return mchName;
	}

	public void setMchName(String mchName) {
		this.mchName = mchName;
	}

	@Override
	public String getMchKey() {
		return mchKey;
	}

	@Override
	public String getPayNotifyUrl() {
		return payNotifyUrl;
	}

	@Override
	public String getRefundNotifyUrl() {
		return this.refundNotifyUrl;
	}

	public String getApiDomain() {
		return apiDomain;
	}

	@Override
	public void setMchKey(String mchKey) {
		this.mchKey = mchKey;
	}

	@Override
	public void setPayNotifyUrl(String notifyUrl) {
		this.payNotifyUrl = notifyUrl;
	}

	@Override
	public void setRefundNotifyUrl(String notifyUrl) {
		this.refundNotifyUrl = notifyUrl;
	}

	/**
	 * 配置 SSL
	 */
	@Override
	public SslContext getSslContext() {
		if (sslContext == null) {
			File file = new File(this.keyPath);
			if (!file.exists()) {
				throw new RuntimeException("证书文件：【" + file.getPath() + "】不存在！");
			}
			try {
				FileInputStream inputStream = new FileInputStream(file);
				KeyStore keystore = KeyStore.getInstance("PKCS12");
				char[] partnerId2charArray = mchId.toCharArray();
				keystore.load(inputStream, partnerId2charArray);
				final KeyManagerFactory kmfactory = KeyManagerFactory
						.getInstance(KeyManagerFactory.getDefaultAlgorithm());
				kmfactory.init(keystore, partnerId2charArray);
				this.sslContext = SslContextBuilder.forClient().keyManager(kmfactory).build();
				return this.sslContext;
			} catch (Exception e) {
				throw new RuntimeException("证书文件有问题，请核实！", e);
			}
		}
		return sslContext;
	}

	/**
	 * 复制
	 */
	public WechatConfig copy() {
		WechatConfigImpl copy = WechatConfigImpl.me();
		copy.appId = this.getAppId();
		copy.secret = this.getSecret();
		copy.token = this.getToken();
		copy.aesKey = this.getAesKey();
		copy.srcId = this.getSrcId();
		copy.setting = this.getSetting();
		copy.mchId = this.getMchId();
		copy.mchKey = this.getMchKey();
		copy.payNotifyUrl = this.getPayNotifyUrl();
		copy.refundNotifyUrl = this.getRefundNotifyUrl();
		copy.apiDomain = this.getApiDomain();
		return copy;
	}

	/**
	 * 只需一个配置
	 * 
	 * @return
	 */
	public static WechatConfigImpl me() {
		return new WechatConfigImpl();
	}
}